import Util from '@/utils/utils.js'
import storage from '@/store/storage'


const common = {
  baseUri: "https://lvyou39.motui.com.cn",
   baseUrl: "https://lvyou39.motui.com.cn/index.php/",
/*   baseUri:'http://localhost',
   baseUrl:'http://localhost/index.php/', */ 
  header: {
    'Content-Type': 'application/json;charset=UTF-8',
  },
  data: {},
  method: 'GET',
  dataType: 'json', //设为 json，会尝试对返回的数据做一次 JSON.parse
  token: uni.getStorageSync('token'),
}
export default {
  //全局配置
  common: common,
  //请求返回Promise对象
  request(options = {}) {
    var that=this;
    //参数处理整合
    options.url = common.baseUrl + options.url
    options.header = options.header || common.header
    options.data = options.data || common.data
    options.method = options.method || common.method
    options.dataType = options.dataType || common.dataType
    options.token = uni.getStorageSync('token') || common.token

    var login_url='/pages/login/login'
    //请求
    return new Promise((res, rej) => {
      // 请求之前看下token标识，登录接口是不需要token(token=false)，此标识将token验证过滤
      if (options.token) {
        let token = options.token
        // 往header头中添加token
        options.header["token"] = token
      }
      //发送请求
      uni.request({
        ...options,
        success: (result) => {
			
          //因为后端返回结果不同意此处没法弄直接返回害
          var res_data=result.data
          if(res_data.code==0){
            Util.shibai(res_data.msg)
            return
          }
	
          if(res_data.code=='401'){
			Util.shibai('请先登录');
			uni.navigateTo({
				url:'/pagesB/pages/my/login'
			})
            return
          }

          return res(res_data)
        },
		error:(err)=>{
			console.log(err)
		},
        fail: (error) => {
			console.log(error)
          Util.shibai(error.errMsg || '请求失败',function(){
            return rej(error)
          });

        }

      })
    })
  },
  // get请求
  get(url, data = {}, options = {}) {
    options.url = url
    options.data = data
    options.method = 'GET'
    return this.request(options)
  },
  // post请求
  post(url, data = {}, options = {}) {
    options.url = url
    options.data = data
    options.method = 'POST'
    return this.request(options)
  },
  // put请求
  put(url, data = {}, options = {}) {
    options.url = url
    options.data = data
    options.method = 'PUT'
    return this.request(options)
  },
  // delete请求
  delete(url, data = {}, options = {}) {
    options.url = url
    options.data = data
    options.method = 'DELETE'
    return this.request(options)
  },
}
